package cn.liukuan.jps.run;

import cn.liukuan.jps.utils.IndexUtils;
import cn.liukuan.jps.utils.WebClientUtils;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by liukuan on 16/5/29.
 */
public class PSearchRun implements Runnable {

    private String href;

    private LinkedBlockingQueue<String> queue;

    public PSearchRun(String href, LinkedBlockingQueue<String> queue){

        this.href = href;
        this.queue = queue;
    }

    public void run() {

        WebClient webClient = WebClientUtils.getWebClient();

        try {
            HtmlPage page = webClient.getPage(href);
            String titleText = page.getTitleText();
            DomNodeList<HtmlElement> ps = page.getBody().getElementsByTagName("p");
            StringBuffer sb = new StringBuffer();
            for (HtmlElement p : ps) {
                sb.append(p.getTextContent()).append("\r\n");
            }
//            System.out.println(sb.toString());

            IndexUtils.indexData(href, titleText, sb.toString());


        } catch (IOException e) {

            e.printStackTrace();

            System.err.println("the url is wrong... " + href );
            try{
                queue.put(href);
            }catch (Exception qe){
            }
            System.err.println("add href " + href + " in queue again.");
        }


    }
}
